package com.mohunhun.service.impl;

import com.mohunhun.mapper.DeptMapper;
import com.mohunhun.pojo.Dept;
import com.mohunhun.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.EmptyStackException;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Dept selectDeptById(Integer id) {
        //校验数据
        if (id == null || id < 0) {
            log.warn("Id数据异常");
            throw new RuntimeException();
        }
        Dept dept = deptMapper.selectDeptById(id);
        //校验部门信息是否存在
        if (dept == null) {
            log.warn("为查到部门信息,id="+id);
            throw new EmptyStackException();
        }
        return dept;
    }

    @Override
    public List<Dept> finAll() {
        return deptMapper.finAll();
    }

    @Override
    public void deleteDeptById(Integer id) {
        //校验数据
        if (id == null || id <= 0) {
            log.warn("Id数据异常");
            throw new EmptyStackException();
        }

        //校验id是否有部门信息
        Dept dept = deptMapper.selectDeptById(id);
        //校验部门信息是否存在
        if (dept == null) {
            log.warn("为查到部门信息,id="+id);
            throw new EmptyStackException();
        }

        //根据id是否能查询出部门数据
        deptMapper.deleteDeptById(id);

    }
}
